package ms.unicorn.impl;

import java.lang.ref.WeakReference;
import java.util.HashMap;
import java.util.Map;

import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.transaction.TransactionalEditingDomain;
import org.eclipse.gmf.runtime.emf.core.GMFEditingDomainFactory;


import ms.unicorn.EditingDomainService;

public class EditingDomainServiceImpl implements EditingDomainService {

	private Map<String, WeakReference<TransactionalEditingDomain>> maps = new HashMap<String, WeakReference<TransactionalEditingDomain>>();
	
	@Override
	public TransactionalEditingDomain getEditingDomain(URI diagramURI) {
		WeakReference<TransactionalEditingDomain> ed = maps.get(diagramURI.toString());
		if (ed != null) {
			if (ed.get() != null)
				return ed.get();
			else 
				maps.remove(diagramURI.toString());
		}
		TransactionalEditingDomain editingDomain = GMFEditingDomainFactory.INSTANCE.createEditingDomain();
		maps.put(diagramURI.toString(), new WeakReference<TransactionalEditingDomain>(editingDomain));
		return editingDomain;
		
	}

}
